package com.geo.ai.model;

import lombok.Data;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * AI模型能力定义
 * 描述AI模型支持的功能和限制
 * 
 * @author GEO Team
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ModelCapability {
    
    // 手动添加builder方法以解决编译问题
    public static ModelCapabilityBuilder builder() {
        return new ModelCapabilityBuilder();
    }
    
    public static class ModelCapabilityBuilder {
        private boolean supportsChat;
        private boolean supportsCompletion;
        private boolean supportsStreaming;
        private boolean supportsFunctionCalling;
        private boolean supportsImageInput;
        private boolean supportsImageGeneration;
        private boolean supportsCodeGeneration;
        private boolean supportsMultiLanguage;
        private Integer maxContextLength;
        private Integer maxOutputLength;
        private Integer maxTokens;
        private List<String> supportedLanguages;
        private List<String> supportedTasks;
        private Map<String, Object> specialAbilities;
        private PerformanceMetrics performance;
        
        public ModelCapabilityBuilder supportsChat(boolean supportsChat) {
            this.supportsChat = supportsChat;
            return this;
        }
        
        public ModelCapabilityBuilder supportsCompletion(boolean supportsCompletion) {
            this.supportsCompletion = supportsCompletion;
            return this;
        }
        
        public ModelCapabilityBuilder supportsStreaming(boolean supportsStreaming) {
            this.supportsStreaming = supportsStreaming;
            return this;
        }
        
        public ModelCapabilityBuilder supportsFunctionCalling(boolean supportsFunctionCalling) {
            this.supportsFunctionCalling = supportsFunctionCalling;
            return this;
        }
        
        public ModelCapabilityBuilder supportsImageInput(boolean supportsImageInput) {
            this.supportsImageInput = supportsImageInput;
            return this;
        }
        
        public ModelCapabilityBuilder supportsImageGeneration(boolean supportsImageGeneration) {
            this.supportsImageGeneration = supportsImageGeneration;
            return this;
        }
        
        public ModelCapabilityBuilder supportsCodeGeneration(boolean supportsCodeGeneration) {
            this.supportsCodeGeneration = supportsCodeGeneration;
            return this;
        }
        
        public ModelCapabilityBuilder supportsMultiLanguage(boolean supportsMultiLanguage) {
            this.supportsMultiLanguage = supportsMultiLanguage;
            return this;
        }
        
        public ModelCapabilityBuilder maxContextLength(Integer maxContextLength) {
            this.maxContextLength = maxContextLength;
            return this;
        }
        
        public ModelCapabilityBuilder maxOutputLength(Integer maxOutputLength) {
            this.maxOutputLength = maxOutputLength;
            return this;
        }
        
        public ModelCapabilityBuilder maxTokens(Integer maxTokens) {
            this.maxTokens = maxTokens;
            return this;
        }
        
        public ModelCapabilityBuilder supportedLanguages(List<String> supportedLanguages) {
            this.supportedLanguages = supportedLanguages;
            return this;
        }
        
        public ModelCapabilityBuilder supportedTasks(List<String> supportedTasks) {
            this.supportedTasks = supportedTasks;
            return this;
        }
        
        public ModelCapabilityBuilder specialAbilities(Map<String, Object> specialAbilities) {
            this.specialAbilities = specialAbilities;
            return this;
        }
        
        public ModelCapabilityBuilder performance(PerformanceMetrics performance) {
            this.performance = performance;
            return this;
        }
        
        public ModelCapability build() {
            ModelCapability capability = new ModelCapability();
            capability.supportsChat = this.supportsChat;
            capability.supportsCompletion = this.supportsCompletion;
            capability.supportsStreaming = this.supportsStreaming;
            capability.supportsFunctionCalling = this.supportsFunctionCalling;
            capability.supportsImageInput = this.supportsImageInput;
            capability.supportsImageGeneration = this.supportsImageGeneration;
            capability.supportsCodeGeneration = this.supportsCodeGeneration;
            capability.supportsMultiLanguage = this.supportsMultiLanguage;
            capability.maxContextLength = this.maxContextLength;
            capability.maxOutputLength = this.maxOutputLength;
            capability.maxTokens = this.maxTokens;
            capability.supportedLanguages = this.supportedLanguages;
            capability.supportedTasks = this.supportedTasks;
            capability.specialAbilities = this.specialAbilities;
            capability.performance = this.performance;
            return capability;
        }
    }
    
    /**
     * 是否支持对话模式
     */
    private boolean supportsChat;
    
    /**
     * 是否支持文本补全
     */
    private boolean supportsCompletion;
    
    /**
     * 是否支持流式输出
     */
    private boolean supportsStreaming;
    
    /**
     * 是否支持函数调用
     */
    private boolean supportsFunctionCalling;
    
    /**
     * 是否支持图像输入
     */
    private boolean supportsImageInput;
    
    /**
     * 是否支持图像生成
     */
    private boolean supportsImageGeneration;
    
    /**
     * 是否支持代码生成
     */
    private boolean supportsCodeGeneration;
    
    /**
     * 是否支持多语言
     */
    private boolean supportsMultiLanguage;
    
    /**
     * 最大上下文长度(Token数)
     */
    private Integer maxContextLength;
    
    /**
     * 最大输出长度(Token数)
     */
    private Integer maxOutputLength;
    
    /**
     * 最大Token数(兼容性字段)
     */
    private Integer maxTokens;
    
    /**
     * 支持的语言列表
     */
    private List<String> supportedLanguages;
    
    /**
     * 支持的任务类型
     */
    private List<String> supportedTasks;
    
    /**
     * 模型特殊能力
     */
    private Map<String, Object> specialAbilities;
    
    /**
     * 性能指标
     */
    private PerformanceMetrics performance;
    
    /**
     * 获取最大Token数(优先使用maxContextLength)
     */
    public Integer getEffectiveMaxTokens() {
        if (maxContextLength != null && maxContextLength > 0) {
            return maxContextLength;
        }
        return maxTokens != null ? maxTokens : 4000;
    }
    
    /**
     * 检查是否支持指定语言
     */
    public boolean supportsLanguage(String language) {
        return supportedLanguages != null && 
               supportedLanguages.contains(language.toLowerCase());
    }
    
    /**
     * 检查是否支持指定任务
     */
    public boolean supportsTask(String taskType) {
        return supportedTasks != null && 
               supportedTasks.contains(taskType.toLowerCase());
    }
    
    /**
     * 获取特殊能力
     */
    public Object getSpecialAbility(String abilityName) {
        return specialAbilities != null ? specialAbilities.get(abilityName) : null;
    }
    
    /**
     * 检查是否具有特殊能力
     */
    public boolean hasSpecialAbility(String abilityName) {
        return specialAbilities != null && specialAbilities.containsKey(abilityName);
    }
    
    /**
     * 性能指标类
     */
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class PerformanceMetrics {
        
        // 手动添加builder方法以解决编译问题
        public static PerformanceMetricsBuilder builder() {
            return new PerformanceMetricsBuilder();
        }
        
        public static class PerformanceMetricsBuilder {
            private Long averageResponseTime;
            private Integer tokensPerSecond;
            private Double accuracy;
            private Double creativity;
            private Double logicalReasoning;
            private Double multiLanguageSupport;
            private Double domainExpertise;
            
            public PerformanceMetricsBuilder averageResponseTime(Long averageResponseTime) {
                this.averageResponseTime = averageResponseTime;
                return this;
            }
            
            public PerformanceMetricsBuilder tokensPerSecond(Integer tokensPerSecond) {
                this.tokensPerSecond = tokensPerSecond;
                return this;
            }
            
            public PerformanceMetricsBuilder accuracy(Double accuracy) {
                this.accuracy = accuracy;
                return this;
            }
            
            public PerformanceMetricsBuilder creativity(Double creativity) {
                this.creativity = creativity;
                return this;
            }
            
            public PerformanceMetricsBuilder logicalReasoning(Double logicalReasoning) {
                this.logicalReasoning = logicalReasoning;
                return this;
            }
            
            public PerformanceMetricsBuilder multiLanguageSupport(Double multiLanguageSupport) {
                this.multiLanguageSupport = multiLanguageSupport;
                return this;
            }
            
            public PerformanceMetricsBuilder domainExpertise(Double domainExpertise) {
                this.domainExpertise = domainExpertise;
                return this;
            }
            
            public PerformanceMetrics build() {
                PerformanceMetrics metrics = new PerformanceMetrics();
                metrics.averageResponseTime = this.averageResponseTime;
                metrics.tokensPerSecond = this.tokensPerSecond;
                metrics.accuracy = this.accuracy;
                metrics.creativity = this.creativity;
                metrics.logicalReasoning = this.logicalReasoning;
                metrics.multiLanguageSupport = this.multiLanguageSupport;
                metrics.domainExpertise = this.domainExpertise;
                return metrics;
            }
        }
        
        /**
         * 平均响应时间(毫秒)
         */
        private Long averageResponseTime;
        
        /**
         * 每秒处理Token数
         */
        private Integer tokensPerSecond;
        
        /**
         * 准确率(0-100)
         */
        private Double accuracy;
        
        /**
         * 创造性评分(0-100)
         */
        private Double creativity;
        
        /**
         * 逻辑性评分(0-100)
         */
        private Double logicalReasoning;
        
        /**
         * 多语言能力评分(0-100)
         */
        private Double multiLanguageSupport;
        
        /**
         * 专业知识评分(0-100)
         */
        private Double domainExpertise;
        
        /**
         * 计算综合评分
         */
        public double calculateOverallScore() {
            double score = 0.0;
            int count = 0;
            
            if (accuracy != null) {
                score += accuracy;
                count++;
            }
            if (creativity != null) {
                score += creativity;
                count++;
            }
            if (logicalReasoning != null) {
                score += logicalReasoning;
                count++;
            }
            if (multiLanguageSupport != null) {
                score += multiLanguageSupport;
                count++;
            }
            if (domainExpertise != null) {
                score += domainExpertise;
                count++;
            }
            
            return count > 0 ? score / count : 0.0;
        }
        
        /**
         * 获取性能等级
         */
        public String getPerformanceLevel() {
            double score = calculateOverallScore();
            if (score >= 90) return "卓越";
            if (score >= 80) return "优秀";
            if (score >= 70) return "良好";
            if (score >= 60) return "一般";
            return "较差";
        }
    }
    
    /**
     * 创建默认的基础能力
     */
    public static ModelCapability createBasicCapability() {
        return ModelCapability.builder()
                .supportsChat(true)
                .supportsCompletion(true)
                .supportsStreaming(false)
                .supportsFunctionCalling(false)
                .supportsImageInput(false)
                .supportsImageGeneration(false)
                .supportsCodeGeneration(true)
                .supportsMultiLanguage(true)
                .maxTokens(4000)
                .supportedLanguages(List.of("zh", "en"))
                .supportedTasks(List.of("content_generation", "keyword_analysis", "qa_generation"))
                .performance(PerformanceMetrics.builder()
                        .accuracy(75.0)
                        .creativity(70.0)
                        .logicalReasoning(75.0)
                        .multiLanguageSupport(80.0)
                        .domainExpertise(70.0)
                        .build())
                .build();
    }
    
    /**
     * 创建高级能力配置
     */
    public static ModelCapability createAdvancedCapability() {
        return ModelCapability.builder()
                .supportsChat(true)
                .supportsCompletion(true)
                .supportsStreaming(true)
                .supportsFunctionCalling(true)
                .supportsImageInput(true)
                .supportsImageGeneration(false)
                .supportsCodeGeneration(true)
                .supportsMultiLanguage(true)
                .maxContextLength(8000)
                .maxOutputLength(4000)
                .supportedLanguages(List.of("zh", "en", "ja", "ko", "fr", "de"))
                .supportedTasks(List.of("content_generation", "keyword_analysis", "qa_generation", 
                                     "sentiment_analysis", "summarization", "translation"))
                .performance(PerformanceMetrics.builder()
                        .accuracy(90.0)
                        .creativity(85.0)
                        .logicalReasoning(88.0)
                        .multiLanguageSupport(92.0)
                        .domainExpertise(85.0)
                        .build())
                .build();
    }
}